< previous page page_78 next page >

Page 78
Metafiles
A Windows metafile (.wmf file) contains a series of graphics instructions that describe an image. While a bitmap (.bmp) file stores an image as a collection of pixel values, a metafile stores instructions for drawing an image using lines, rectangles, ellipses, and so forth. This approach makes metafiles more compact than bitmap files for many types of images.
Many other applications, like drawing packages, can load and save Windows metafiles. You can use this fact to import images drawn by your programs into a drawing package. Many of the illustrations in this book started as metafiles created by a program. I loaded the metafiles into a drawing program, scaled, edited, and added captions to them and then saved the results in a different format for publication.
Like other API routines, metafile functions work only in pixels. You must measure distances in pixels when you are drawing into a metafile. Similarly, when you play a metafile back into a form or picture box, you draw the image using pixels whether you normally measure distances in pixels, twips, or some other unit.
You can create a new metafile using the CreateMetaFile function. This function takes as a parameter the name of the file you want to create. If you set this parameter to vbNullString, the function creates a metafile in memory rather than in a file. This approach can be useful if you want to draw the same image in several different places in your program. You can create a memory metafile and play it back into several different forms or picture boxes without creating the file on your hard disk.
Most programs that import metafiles expect the metafile to include information about how large it should be. If you do not include this information, an application importing the metafile may make its picture extremely small. Metafiles contain instructions for drawing rather than bitmapped images, so you can probably resize the image to make it bigger.
You can make an imported image start at a more reasonable size using the SetWidowExtEx API function. This routine includes information about the drawing's size in the metafile, so the importing program can give the image a reasonable size.
The CreateMetaFile function returns a device context handle (hDC) for the new metafile. You can use this handle as the device context parameter for many API drawing routines including Arc, Chord, Ellipse, FloodFill, LineTo, MoveToEx, Pie, Rectangle, and RoundRect.
After you place drawing commands in the metafile using the API drawing routines, close the metafile using the CloseMetaFile function. This function returns a handle to the newly created metafile. This is different from the handle to the file's device context that is returned by the CreateMetaFile function. You use the device context handle to draw into the metafile. You use the metafile handle to play the metafile back if you like.
You can obtain a handle to an existing metafile using the GetMetaFile function. For example, if you previously created a metafile, you can use GetMetaFile to open the file to display it later.
Once you have a handle to a metafile, obtained from either CloseMetaFile or GetMetaFile, you can play the file's drawing commands back into a form or picture box

 
< previous page page_78 next page >